package com.ipzoe.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.ipzoe.app.dto.ArticleCategoryResponseDTO;
import com.ipzoe.app.dto.ArticleResponseDTO;
import com.ipzoe.cms.domain.Agreement;
import com.ipzoe.cms.domain.Article;
import com.ipzoe.cms.domain.ArticleCategory;
import com.ipzoe.cms.service.IAgreementService;
import com.ipzoe.cms.service.IArticleCategoryService;
import com.ipzoe.cms.service.IArticleService;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/cms/article")
@Slf4j
@Validated
@Api(tags = "文章相关")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IArticleCategoryService iArticleCategoryService;
    @Autowired
    private IAgreementService iAgreementService;

    @GetMapping("/category/list")
    @ApiOperation("查询所有的文章类别")
    public AjaxResult list()
    {
        LambdaQueryWrapper<ArticleCategory> categoryLqw = new LambdaQueryWrapper<>();
        categoryLqw.orderByAsc(ArticleCategory::getOrderNum);
        List<ArticleCategory> list = iArticleCategoryService.list(categoryLqw);

        return AjaxResult.success(list);

    }

    @ApiOperation("获取文章明细")
    @GetMapping("/detail/{articleId}")
    public AjaxResult detail(@PathVariable Long articleId){

        Article article = iArticleService.getById(articleId);
        return AjaxResult.success(article);
    }

    @GetMapping("/list/{categoryId}")
    @ApiOperation("查询某一分类下面的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "query", defaultValue = "1")
    })
    public AjaxResult list(@PathVariable Long categoryId,
                           @RequestParam(defaultValue = "1",required = false) int pageNum)
    {
        int pageSize = 10;
        Page<Article> page = new Page<Article>(pageNum, pageSize);

//        ArticleCategory articleCategory = iArticleCategoryService.getById(categoryId);
//        if(articleCategory == null){
//            return AjaxResult.success(page);
//        }

        LambdaQueryWrapper<Article> articleLqw = new LambdaQueryWrapper<>();
        articleLqw.eq(Article::getCategoryId, categoryId)
                .orderByAsc(Article::getOrderNum)
                .orderByDesc(Article::getCreateTime);

        page = iArticleService.page(page, articleLqw);

        return AjaxResult.success(page);
    }

    @ApiOperation("获取协议")
    @GetMapping("/agreement/{id}")
    public AjaxResult agreement(@PathVariable Long id){

        Agreement agreement = iAgreementService.getById(id);
        return AjaxResult.success(agreement);
    }
}
